using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using AjaxControlToolkit;
using System.Drawing.Design;

[assembly: System.Web.UI.WebResource("AjaxCtrlsExtenders.MultiSelectDropDownComboBox.MultiSelectDropDownComboBoxBehavior.js", "text/javascript")]

namespace AjaxCtrlsExtenders
{
    // do I need to parse children?
    // do I need to persis children?
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:MultiSelectDropDownComboBox runat=\"server\" width=\"138px\" textboxwidth=\"100px\" buttonwidth=\"32px\" listboxwidth=\"138px\"></{0}:MultiSelectDropDownComboBox>")]
    [ClientScriptResource("AjaxCtrlsExtenders.MultiSelectDropDownComboBox", "AjaxCtrlsExtenders.MultiSelectDropDownComboBox.MultiSelectDropDownComboBoxBehavior.js")]
    //[System.Drawing.ToolboxBitmap(typeof(MultiSelectDropDownComboBox), "SomeIcon.ico")]
    [Designer(typeof(MultiSelectDropDownComboBoxDesigner))]
    public partial class MultiSelectDropDownComboBox : CompositeControl, INamingContainer
    {
        static public double defaultWidth = 139;
        static public double defaultButtonWidth = 32;
        static public double defaultTextboxWidth = 100;
        static public double defaultListboxWidth = 139;
        static public Unit defaultHeight = Unit.Empty;
        static public Unit defaultTextboxHeight = Unit.Empty;

        //private Panel ctrlPanelOuter;
        public Panel ctrlPanel;
        public TextBox ctrlTextbox;
        public System.Web.UI.WebControls.Image ctrlButton;
        public ListBox ctrlListbox;

        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Text
        {
            get
            {
                EnsureChildControls();
                if (ViewState["text"] != null)
                {
                    ctrlTextbox.Text = (string)ViewState["text"];
                }
                return ctrlTextbox.Text;
            }

            set
            {
                EnsureChildControls();
                ViewState["text"] = value ?? "";
                ctrlTextbox.Text = value;
            }
        }

        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.Attribute)]
        public override Color BackColor
        {
            get
            {
                EnsureChildControls();
                if (ViewState["BackColor"] != null)
                {
                    base.BackColor = (Color)ViewState["BackColor"];
                }
                return base.BackColor;
            }

            set
            {
                EnsureChildControls();
                ViewState["BackColor"] = value;
                base.BackColor = value;
            }
        }

        [Category("Layout")]
        [DefaultValue("")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        public override Unit Width
        {
            get
            {
                EnsureChildControls();
                if (ViewState["Width"] != null)
                {
                    base.Width = (Unit)ViewState["Width"];
                }
                else
                {
                    base.Width = new Unit(MultiSelectDropDownComboBox.defaultWidth, UnitType.Pixel);
                    ViewState["Width"] = base.Width;
                }
                return base.Width;
            }

            set
            {
                EnsureChildControls();
                ViewState["Width"] = value;
                base.Width = value;
            }
        }
        [Category("Layout")]
        [DefaultValue("")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        [Browsable(true)]
        //[ReadOnly(true)]
        public override Unit Height
        {
            get
            {
                EnsureChildControls();
                if (ViewState["Height"] != null)
                {
                    base.Height = (Unit)ViewState["Height"];
                }
                else
                {
                    base.Height = MultiSelectDropDownComboBox.defaultHeight;
                    ViewState["Height"] = base.Height;
                }
                return base.Height;
            }

            set
            {
                EnsureChildControls();
                ViewState["Height"] = value;
                base.Height = value;
            }
        }

#region Inner Text Box Properties

        [Category("Appearance")]
        [DefaultValue("")]
        [Description("Textbox Background Color")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        public Color TextboxBackColor
        {
            get
            {
                EnsureChildControls();
                if (ViewState["TextboxBackColor"] != null)
                {
                    ctrlTextbox.BackColor = (Color)ViewState["TextboxBackColor"];
                }
                return ctrlTextbox.BackColor;
            }

            set
            {
                EnsureChildControls();
                ViewState["TextboxBackColor"] = value;
                ctrlTextbox.BackColor = value;
            }
        }


        [Category("Layout")]
        [DefaultValue("")]
        [PersistenceMode(PersistenceMode.Attribute)]
        [Description("Textbox width.")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        public Unit TextboxWidth
        {
            get
            {
                EnsureChildControls();
                if (ViewState["TextboxWidth"] != null)
                {
                    ctrlTextbox.Width = (Unit)ViewState["TextboxWidth"];
                }
                else
                {
                    base.Width = new Unit(MultiSelectDropDownComboBox.defaultTextboxWidth, UnitType.Pixel);
                    ViewState["TextboxWidth"] = base.Width;
                }

                return ctrlTextbox.Width;
            }

            set
            {
                EnsureChildControls();
                ViewState["TextboxWidth"] = value;
                ctrlTextbox.Width = value;
            }
        }

        [Category("Layout")]
        [DefaultValue("")]
        [PersistenceMode(PersistenceMode.Attribute)]
        [Description("Textbox Height.")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        public Unit TextboxHeight
        {
            get
            {
                EnsureChildControls();
                if (ViewState["TextboxHeight"] != null)
                {
                    ctrlTextbox.Height = (Unit)ViewState["TextboxHeight"];
                }
                else
                {
                    ctrlTextbox.Height = MultiSelectDropDownComboBox.defaultTextboxHeight;
                    ViewState["TextboxHeight"] = ctrlTextbox.Height;
                }

                return ctrlTextbox.Height;
            }

            set
            {
                EnsureChildControls();
                ViewState["TextboxHeight"] = value;
                ctrlTextbox.Height = value;
            }
        }

        #endregion

        #region Inner Button Properties


        [Category("Layout")]
        [DefaultValue("")]
        [Description("Button width.")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        public Unit ButtonWidth
        {
            get
            {
                EnsureChildControls();
                if (ViewState["ButtonWidth"] != null)
                {
                    ctrlButton.Width = (Unit)ViewState["ButtonWidth"];
                }
                else
                {
                    base.Width = new Unit(MultiSelectDropDownComboBox.defaultButtonWidth, UnitType.Pixel);
                    ViewState["ButtonWidth"] = base.Width;
                }
                return ctrlButton.Width;
            }

            set
            {
                EnsureChildControls();
                ViewState["ButtonWidth"] = value;
                ctrlButton.Width = value;
               
            }
        }

        //private string _InnerButtonUpUrl;
        //private string _InnerButtonDownUrl;

        //[Bindable(true)]
        [Browsable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Description("Button Up Image URL")]
        [Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(UITypeEditor))]
        [Localizable(true)]
        [NotifyParentProperty(true)]
        public string ButtonUpImage
        {
            get
            {
                EnsureChildControls();
                if (ViewState["ButtonUpImage"] != null)
                {
                    ctrlButton.ImageUrl = (string)ViewState["ButtonUpImage"];
                }
                return ctrlButton.ImageUrl;
            }

            set
            {
                EnsureChildControls();
                ViewState["ButtonUpImage"] = value;
                ctrlButton.ImageUrl = value;
            }
        }
        
        //[Bindable(true)]
        [Category("Appearance")]
        [Browsable(true)]
        [DefaultValue("")]
        [Description("Button Down Image URL")]
        [Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(UITypeEditor))]
        [Localizable(true)]
        [NotifyParentProperty(true)]
        public string ButtonDownImage
        {
            get
            {
                EnsureChildControls();
                if (ViewState["ButtonDownImage"] != null)
                {
                    ctrlButton.ImageUrl = (string)ViewState["ButtonDownImage"];
                }
                return ctrlButton.ImageUrl;
            }

            set
            {
                EnsureChildControls();
                ViewState["ButtonDownImage"] = value;
                ctrlButton.ImageUrl = value;
            }
        }

        [Category("Appearance")]
        [DefaultValue("")]
        [Description("Button Background Color")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        public Color ButtonBackColor
        {
            get
            {
                EnsureChildControls();
                if (ViewState["ButtonBackColor"] != null)
                {
                    ctrlButton.BackColor = (Color)ViewState["ButtonBackColor"];
                }

                return ctrlButton.BackColor;
            }

            set
            {
                EnsureChildControls();
                ViewState["ButtonBackColor"] = value;
                ctrlButton.BackColor = value;
            }
        }

        [Category("Layout")]
        [DefaultValue("")]
        [Description("Button Image alignment")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        public EnumButtonAlignment ButtonAlignment
        {
            get
            {
                EnsureChildControls();
                EnumButtonAlignment ba = EnumButtonAlignment.RIGHT;
                if (ViewState["ButtonAlignment"] != null)
                {
                    ba = (EnumButtonAlignment)ViewState["ButtonAlignment"];
                }
                return ba;
            }

            set
            {
                EnsureChildControls();
                ViewState["ButtonAlignment"] = value;
            }

        }
        #endregion

        #region Listbox Properties

        [Category("Appearance")]
        [DefaultValue("")]
        [Description("ListboxBackColor Background Color")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        public Color ListboxBackColor
        {
            get
            {
                EnsureChildControls();
                if (ViewState["ListboxBackColor"] != null)
                {
                    ctrlListbox.BackColor = (Color)ViewState["ListboxBackColor"];
                }

                return ctrlListbox.BackColor;
            }

            set
            {
                EnsureChildControls();
                ViewState["ListboxBackColor"] = value;
                ctrlListbox.BackColor = value;
            }
        }

        [Category("Layout")]
        [DefaultValue("")]
        [Description("Listbox width.")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        public Unit ListboxWidth
        {
            get
            {
                EnsureChildControls();
                if (ViewState["ListboxWidth"] != null)
                {
                    ctrlListbox.Width = (Unit)ViewState["ListboxWidth"];
                }
                else
                {
                    ctrlListbox.Width = new Unit(MultiSelectDropDownComboBox.defaultListboxWidth, UnitType.Pixel);
                    ViewState["ListboxWidth"] = base.Width;
                }
                return ctrlListbox.Width;
            }

            set
            {
                EnsureChildControls();
                ViewState["ListboxWidth"] = value;
                ctrlListbox.Width = value;
            }
        }

        [Category("Behavior")]
        [DefaultValue("")]
        [Description("Listbox visible.")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [NotifyParentProperty(true)]
        public bool ListboxVisible
        {
            get
            {
                EnsureChildControls();
                if (ViewState["ListboxVisible"] != null)
                {
                    ctrlListbox.Visible = (bool)ViewState["ListboxVisible"];
                }
                return ctrlListbox.Visible;
            }

            set
            {
                EnsureChildControls();
                ViewState["ListboxVisible"] = value;
                ctrlListbox.Visible = value;
            }
        }        
#endregion


        public MultiSelectDropDownComboBox():base()
        {
        }

        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }

        public enum EnumButtonAlignment {
            RIGHT,
            LEFT
        }
        protected override void RecreateChildControls()
        {
            //base.RecreateChildControls();
            base.ChildControlsCreated = false;
            EnsureChildControls();
        }
        protected override void CreateChildControls()
        {
            Controls.Clear();
            ctrlPanel = new Panel();
            // LISTBOX
            ctrlListbox = new ListBox();
            //ctrlListbox.Style.Add("display", "none");

            //TEXTBOX
            ctrlTextbox = new TextBox();
            //ctrlTextbox.Width = new Unit(5, UnitType.Pixel);

            //BUTTON
            ctrlButton = new ImageButton();
            ctrlButton.Attributes.Add("onclick","javascript:alert(\"clicked\");");
            ctrlButton.ImageAlign = ImageAlign.AbsMiddle;


            ctrlButton.ImageUrl = "buttonDownArrow.jpg";
            ctrlButton.BackColor = this.ButtonBackColor;
            ctrlButton.Width = this.ButtonWidth;

            ctrlTextbox.BackColor = this.TextboxBackColor;
            ctrlTextbox.Width = this.TextboxWidth;

            ctrlListbox.Visible = true;
            ctrlListbox.BackColor = this.ListboxBackColor;
            ctrlListbox.Width = this.ListboxWidth;

            ctrlPanel.Controls.Add(ctrlTextbox);
            ctrlPanel.Controls.Add(ctrlButton);
            Controls.Add(ctrlPanel);
            Controls.Add(ctrlListbox);
        }
        
        public override Control FindControl(string id)
        {
            EnsureChildControls();
            return base.FindControl(id);
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            ctrlPanel.RenderBeginTag(output);
            switch (this.ButtonAlignment)
            {
                case EnumButtonAlignment.RIGHT:
                    ctrlTextbox.RenderControl(output);
                    ctrlButton.RenderControl(output);
                    break;
                case EnumButtonAlignment.LEFT:
                    ctrlButton.RenderControl(output);
                    ctrlTextbox.RenderControl(output);
                    break;
            }
            ctrlPanel.RenderEndTag(output);
            if (DesignMode)
            {
                if (ListboxVisible)
                {
                    ctrlListbox.RenderControl(output);
                }
            }
            else
            {
                ctrlListbox.RenderControl(output);
            }
        }
       
    }
}
